<!DOCTYPE html>
<html lang="en">
<head>
    <title>朝圣"菱"距离</title>
    <meta charset="UTF-8">
    <!-- <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/> -->
    
    
    
    
    <script type="text/javascript" src="js/jquery.min.js"></script>
	<script type="text/javascript" src="../../../js/common/common.js"></script>
	<script type="text/javascript" src="../../../js/common/wechatCommon.js"></script>
	
</head>


<body>
    <script type="text/javascript">
    
    	var rootPath = getRootPath();
    	
    	$(document).ready(function(){
    		init();
		});
    	
    	function init(){
    		$.ajax({
     			headers:{'Cookie':document.cookie},
				type:"POST", 
		        //url:"http://127.0.0.1:9013/pointActivity/api/v1/queryQuestion",
		        url:rootPath + "/pointActivity/api/v1/queryQuestion",
		        dataType:"json",
		        contentType:"application/json",
		        success:function(data){
		        	console.log(data);
		        	if(data.qtnNo != null){
		        		$("h1").html(data.qtnNo+"."+data.qtnTitle);
		        		for(var i = 0; i < data.list.length; i++){
		        			if(data.qtId == 1){
		        				$("#answer").append("<input type='checkbox' name='a' value='"+data.list[i].answerId+"'/>"+data.list[i].answer+"<br>");
			        		}
			        		else if(data.qtId == 2){
			        			if(data.list[i].answer == "其他"){
		        					$("#answer").append("<input type='radio' name='answerId' value='"+data.list[i].answerId+"'/>"+data.list[i].answer+"<input type='text'/><br>");
		        				}else{
		        					$("#answer").append("<input type='radio' name='answerId' value='"+data.list[i].answerId+"' />"+data.list[i].answer+"<br>");
		        				}
			        		}
			        		else{
			        			$("#answer").append("<textarea rows='20' cols='120' />");
			        		}
		        		}
		        		if(data.qtnNo != 1){
		        			$("#last").append("<input type=\"button\" value=\"上一题\" onclick='toLast("+JSON.stringify(data)+");'/>");
		        		}
		        		if(data.qtnNo ==2 || data.qtnNo == 12){
		        			$("#next").append("<input type=\"button\" value=\"提交问卷\" onclick='toNext("+JSON.stringify(data)+");'/>");
		        		}
		        		else{
		        			$("#next").append("<input type=\"button\" value=\"下一题\" onclick='toNext("+JSON.stringify(data)+");'/>");
		        		}
		        	}
		        	else{
		        		alert("跳转抽奖页面！");
		        	}
		        }
		     });
    	}
    	
    	function toNext(data){
    		console.log(data);
    		var answer = "";
    		var url = "";
    		var answerId = "";
    		if(data.qtId == 2){
    			answer = $('input:radio:checked').val();
    			if(answer != null && answer != ""){
    				var answerTitle = $('input:radio:checked')[0].nextSibling.nodeValue;
        			if(answerTitle == "其他"){
        				answer = $('input:text').val();
        			}
        			if(answer == null || answer ==""){
        				alert("请填写其他内容！");
        				return;
        			}
        			answerId = $('input:radio:checked').val();
    				url = rootPath + "/pointActivity/api/v1/queryNextQuestionByQtnNo?qtnNo="+data.qtnNo+"&qtnId="+data.qtnId+"&answer="+answer+"&qneId="+data.qneId+"&qId="+data.qId+"&ownerId="+data.ownerId+"&answerId="+answerId;
    				goNext(data,url);
    			}else{
    				alert("您还没选中任何内容！");
    				return;
    			}
    		}
    		else if(data.qtId ==1){
    			var answerArr =[]; 
    			$('input[name="a"]:checked').each(function(){ 
    				answerArr.push($(this).val()); 
    			}); 
    			if(answerArr.length == 0){
    				alert("您还没选中任何内容！");
    				return;
    			}
    			else if(answerArr.length > 3){
    				alert("最多选择三项！");
    				return;
    			}
    			else{
    				for(var i = 0; i < answerArr.length; i++){
    					answer = answer + answerArr[i] +",";
    					}
    				answer = answer.substring(0,answer.length-1);
    				console.log(answer);
    				url = rootPath + "/pointActivity/api/v1/queryNextQuestionByQtnNo?qtnNo="+data.qtnNo+"&qtnId="+data.qtnId+"&qneId="+data.qneId+"&answer="+answer+"&qId="+data.qId+"&ownerId="+data.ownerId;
    				goNext(data,url);	
    				}
    			}
    		else{
    			answer = $('textarea').val();
    			console.log(answer);
    			if(answer == null || answer == ""){
    				alert("您还没填写任何内容！");
    			}
    			else{
    				url = rootPath + "/pointActivity/api/v1/queryNextQuestionByQtnNo?qtnNo="+data.qtnNo+"&qtnId="+data.qtnId+"&qneId="+data.qneId+"&answer="+answer+"&qId="+data.qId+"&ownerId="+data.ownerId;
    				goNext(data,url);
    			}
    		}
    	}
    	
    	function toLast(data){
    		$("#answer").empty();
    		$("#next").empty();
    		$("#last").empty();
    		console.log(data);
    		$.ajax({
    			headers:{'Cookie':document.cookie},
    			type:"POST", 
		        //url:"http://127.0.0.1:9013/pointActivity/api/v1/queryLastQuestionByQtnNo?qtnNo="+data.qtnNo+"&qId="+data.qId+"&ownerId="+data.ownerId+"&qtnId="+data.qtnId+"&qneId="+data.qneId,
		        url:rootPath + "/pointActivity/api/v1/queryLastQuestionByQtnNo?qtnNo="+data.qtnNo+"&qId="+data.qId+"&ownerId="+data.ownerId+"&qtnId="+data.qtnId+"&qneId="+data.qneId,
		        dataType:"json",
		        contentType:"application/json",
		        success:function(data){
		        	console.log(data);
		        	if(data != null){
		        		$("h1").html(data.qtnNo+"."+data.qtnTitle);
		        		for(var i = 0; i < data.list.length; i++){
		        			if(data.qtId == 1){
		        				console.log(data.userAnswer)
		        				console.log(data.list[i].answerId)
		        				if(isContains(data.userAnswer,data.list[i].answerId)){
		        					$("#answer").append("<input type='checkbox' name='a' checked='checked' value='"+data.list[i].answerId+"'/>"+data.list[i].answer+"<br>");
		        				}else{
		        					$("#answer").append("<input type='checkbox' name='a'  value='"+data.list[i].answerId+"'/>"+data.list[i].answer+"<br>");
		        				}
		        				
			        		}
			        		else if(data.qtId == 2){
			        			if(data.list[i].answer == "其他"){
			        				if(data.list[i].answerId == data.remark){
			        					$("#answer").append("<input type='radio' name='answerId' checked='checked' value='"+data.list[i].answerId+"'/>"+data.list[i].answer+"<input type='text' value='"+data.userAnswer+"'/><br>");
			        				}else{
			        					$("#answer").append("<input type='radio' name='answerId' value='"+data.list[i].answerId+"'/>"+data.list[i].answer+"<input type='text'/><br>");
			        				}
		        				}else{
		        					if(data.list[i].answerId == data.remark){
		        						$("#answer").append("<input type='radio' name='answerId' checked='checked' value='"+data.list[i].answerId+"' />"+data.list[i].answer+"<br>");
		        					}else{
		        						$("#answer").append("<input type='radio' name='answerId' value='"+data.list[i].answerId+"' />"+data.list[i].answer+"<br>");
		        					}
		        				}
			        			
			        		}
			        		else{
			        			$("#answer").append("<textarea rows='20' cols='120' />");
			        		}
		        		}
		        		if(data.qtnNo != 1){
		        			$("#last").append("<input type=\"button\" value=\"上一题\" onclick='toLast("+JSON.stringify(data)+");'/>");
		        		}
		        		$("#next").append("<input type=\"button\" value=\"下一题\" onclick='toNext("+JSON.stringify(data)+");'/>");
		        	}
		        }
		     });
    	}
    	
    	function goNext(data,url){
    		$("#answer").empty();
    		$("#next").empty();
    		$("#last").empty();
    		$.ajax({
    			headers:{'Cookie':document.cookie},
    			type:"POST", 
		        url:url,
		        dataType:"json",
		        contentType:"application/json",
		        success:function(data){
		        	console.log(data);
		        	if(data.qtnNo != null){
		        		$("h1").html(data.qtnNo+"."+data.qtnTitle);
		        		var str = "";
		        		for(var i = 0; i < data.list.length; i++){
		        			if(data.qtId == 1){
		        				if(data.userAnswer != null && data.userAnswer != ""){
		        					str = data.userAnswer;
		        				}
		        				if(isContains(str,data.list[i].answerId)){
		        					$("#answer").append("<input type='checkbox' name='a' checked='checked' value='"+data.list[i].answerId+"'/>"+data.list[i].answer+"<br>");
		        				}else{
		        					$("#answer").append("<input type='checkbox' name='a'  value='"+data.list[i].answerId+"'/>"+data.list[i].answer+"<br>");
		        				}
			        		}
			        		else if(data.qtId == 2){
			        			if(data.list[i].answer == "其他"){
			        				if(data.list[i].answerId == data.remark){
			        					$("#answer").append("<input type='radio' name='answerId' checked='checked' value='"+data.list[i].answerId+"'/>"+data.list[i].answer+"<input type='text' value='"+data.userAnswer+"'/><br>");
			        				}else{
			        					$("#answer").append("<input type='radio' name='answerId' value='"+data.list[i].answerId+"'/>"+data.list[i].answer+"<input type='text'/><br>");
			        				}
		        				}else{
		        					if(data.list[i].answerId == data.remark){
		        						$("#answer").append("<input type='radio' name='answerId' checked='checked' value='"+data.list[i].answerId+"' />"+data.list[i].answer+"<br>");
		        					}else{
		        						$("#answer").append("<input type='radio' name='answerId' value='"+data.list[i].answerId+"' />"+data.list[i].answer+"<br>");
		        					}
		        				}
			        		}
			        		else{
			        			$("#answer").append("<textarea rows='20' cols='120' />");
			        		}
		        		}
		        		$("#last").append("<input type=\"button\" value=\"上一题\" onclick='toLast("+JSON.stringify(data)+");'/>");
		        		if(data.qtnNo ==2 || data.qtnNo == 12){
		        			$("#next").append("<input type=\"button\" value=\"提交问卷\" onclick='toNext("+JSON.stringify(data)+");'/>");
		        		}
		        		else{
		        			$("#next").append("<input type=\"button\" value=\"下一题\" onclick='toNext("+JSON.stringify(data)+");'/>");
		        		}
		        	}
		        	else{
		        		alert("跳转抽奖页面!");
		        	}
		        }
		     });
    	}
    	
    	
    	function isContains(str, substr) {
    	    return str.indexOf(substr) >= 0;
    	}
    	
    	
    </script>
    
    <h1 id="question"></h1>
    <div id="answer"></div>
    <div id="last"></div>
    <div id="next"></div>
</body>
</html>